package tmpl

import (
	"gitee.com/lllgo/gopkg/template"
	"gitee.com/lllgo/opsctl/pkg/cmd"
	"gitee.com/lllgo/opsctl/pkg/conf"
	"github.com/mitchellh/mapstructure"
)

func init() {
	cmd.RegisterCmdPlugin("tmpl", func(pc conf.PluginConfig, c conf.ComponentConfig) error {
		var tmplCfg template.Config
		if err := mapstructure.Decode(c, &tmplCfg); err != nil {
			return err
		}
		// 模板配置文件位置默认相对configpath
		if pc.Dir == "" {
			tmplCfg.BasePath = conf.ConfigPath
		} else {
			tmplCfg.BasePath = pc.Dir
		}
		return tmplCfg.Write()
	}, conf.PluginConfig{
		Desc:     "go模板渲染命令",
		Cmd:      "tmpl",
		Register: "register.yaml",
	})

}
